<?php

namespace frontend\modules\tsline\controllers;

use Yii;
use frontend\components\Controller;
use frontend\modules\tsline\models\TslineLikeForm;
use frontend\modules\tsline\models\ArticleCollectForm;

/**
 * 泰山号扩展功能接口
 * ExtendController controller for the `tsline` module
 */
class ExtendController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            '*' => ['POST']
        ];
    }

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * 点赞&取消点赞
     * @return string
     */
    public function actionLike() {
        $model = new TslineLikeForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            return $model->dumpError('点赞请求失败，请检查传入参数有效性');
        }
        $model->user_id = $this->token->user_id;
        $model->device_id = $this->token->device_id;
        return $model->save();
    }

    /**
     * 收藏接口
     * @return string
     */
    public function actionCollect() {
        $model = new ArticleCollectForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            return $model->dumpError('收藏失败，请检查传入参数有效性');
        }
        $model->user_id = $this->token->user_id;
        return $model->save();
    }

}
